<template>
  <view class="library">
    <view class="setup-box">
      <text class="setup-title">个人画像</text>
      <Radar
        :width="700"
        id="htzyRadarId"
        :categories="radarCategories"
        :chartData="radarChartData"
      />
    </view>
    <view class="setup-box">
      <text class="setup-title">基本信息</text>
      <uni-list>
        <uni-list-item title="岗位">
          <template #footer>
            <up-input
              placeholder="请输入岗位"
              border="none"
              inputAlign="right"
              v-model="userInfo.post"
            ></up-input>
          </template>
        </uni-list-item>

        <uni-list-item title="行业" :rightText="userInfo.industryBigType" >
        </uni-list-item>
        <uni-list-item
          @click="xue"
          title="学历"
          :rightText="userInfo.education"
          link
        />
        <uni-list-item
          @click="zhi"
          title="职称"
          :rightText="userInfo.jobTitle"
          link
        />
      </uni-list>
      <up-picker
        :show="educationShow"
        :defaultIndex="educationIndex"
        key-name="label"
        title="学历"
        :columns="educationData"
        @confirm="handlerEducationConfirm"
      ></up-picker>
      <up-picker
        :show="academicShow"
        :defaultIndex="academicIndex"
        key-name="label"
        title="职称等级"
        :columns="academicData"
        @confirm="handlerAcademicConfirm"
      ></up-picker>
      <view class="room-btn">
        <button class="btn" form-type="submit" @tap="submit">提交</button>
      </view>
    </view>
  </view>
</template>

<script setup>
import Radar from "@/package_user/pages/user/components/radar.vue";
import { getUserInfoApi } from "@/api/user";
import { generateRandomNumId } from "@/utils/utils";
import {
  getUserHonorScoreApi,
  updateUserInfoApi,
} from "@/package_user/pages/api/library.js";
import { getConfigEnterpriseFieldsApi } from "@/api/honour/search";
import { ref } from "vue";
import { onShow } from "@dcloudio/uni-app";
import { flat } from "vant/lib/utils";
const radarCategories = ref([]);
/* 雷达图数据 */
const radarChartData = ref([
  {
    name: "个人指数",
    data: [],
  },
]);
const educationShow = ref(false);
const academicShow = ref(false);
const educationData = ref([]);
const academicData = ref([]);
const educationIndex = ref([0]);
const academicIndex = ref([0]);
const userInfo = ref({});
const cellList = ref([]);
const getUserInfo = async () => {
  const res = await getUserInfoApi();
  const { code, data } = res;
  if (code == 200) {
    userInfo.value = data;
    console.log(userInfo.value);
    getUserHonorScore();
  }
};
const xue = (data) => {
  console.log(data);
  educationShow.value = true;
};
const zhi = (data) => {
  console.log(data);
  academicShow.value = true;
};
// 个人雷达图
const getUserHonorScore = async () => {
  let res = await getUserHonorScoreApi(userInfo.value.id);
  console.log(res);
  const { code, data } = res || {};
  if (code == 200) {
    const { honorUserScoreVo, honorScoreMaxVo, totalScore } = data || {};
    console.log("honorUserScoreVo", honorUserScoreVo);
    console.log("honorScoreMaxVo", honorScoreMaxVo);
    console.log("totalScore", totalScore);
    // console.log("honorScoreStageConfigEntity",honorScoreStageConfigEntity);
    const gradeMap = {
      专业力: "zylGrade",
      进取心: "jqxGrade",
      责任心: "zrxGrade",
      创新力: "cxlGrade",
      敬业心: "jyxGrade",
      沟通力: "gtlGrade",
    };
    const dimListMap = {
      专业力: "zylList",
      进取心: "jqxList",
      责任心: "zrxList",
      创新力: "cxlList",
      敬业心: "jyxList",
      沟通力: "gtlList",
    };
    const gradeArr = ["C", "B", "A", "A+", "S"];
    const average = honorUserScoreVo;
    Array.isArray(honorScoreMaxVo) &&
      honorScoreMaxVo.forEach((dimension, i) => {
        let gradeAverage = data[gradeMap[dimension.dimensionName]] || "";
        radarCategories.value.push(
          dimension.dimensionName + "\n" + gradeAverage
        );
        let allScore = gradeArr.findIndex((item) => item === gradeAverage) + 1;
        radarChartData.value[0].data.push((allScore * 2) / 10);
      });
  }
};
// 获取下拉菜单数据
const getConfigEnterpriseFields = async () => {
  const params = {
    type: 2,
  };
  const res = await getConfigEnterpriseFieldsApi(params);
  const { code, data } = res;
  if (code === 200) {
    cellList.value = data.map((item) => {
      return item.children.map((v, i) => {
        return {
          id: i,
          label: v.filedName,
        };
      });
    });
    educationData.value = [cellList.value[0]];
    academicData.value = [cellList.value[1]];
  }
};
// 学历确认按钮
const handlerEducationConfirm = (data) => {
  console.log(data);
  userInfo.value.education = data.value[0].label;
  educationShow.value = false;
};
// 职称确认按钮
const handlerAcademicConfirm = (data) => {
  console.log(data);
  userInfo.value.jobTitle = data.value[0].label;
  academicShow.value = false;
};
const submit = async () => {
  if (
    userInfo.value.education !== "" ||
    userInfo.value.education !== undefined ||
    userInfo.value.education !== null
  ) {
    const params = {
      education: userInfo.value.education,
      post: userInfo.value.post,
      jobTitle: userInfo.value.jobTitle,
    };
    const res = await updateUserInfoApi(params);
    const { code, data } = res;
    if (code == 200) {
      console.log(data);
      await uni.showToast({
        title: "提交成功",
        duration: 2000,
      });
    }
  } else {
  }
};
onShow(() => {
  getConfigEnterpriseFields();
  getUserInfo();
});
</script>

<style lang="less" scoped>
@import url(./less/setup.less);
</style>